<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thinhhung
 * Date: 5/31/12
 * Time: 3:52 PM
 * To change this template use File | Settings | File Templates.
 */

class HoaDonBansController extends AppController
{
    public $helper = array('Html', 'Form');
    public $name = 'HoaDonBans';


    var $paginate = array(
        'limit' => '7',
        'order' => array(
            'hoadonban.id' => 'asc',
            'hoadonban.MAHDB' => 'asc'
        )
    );

    public function index()
    {
        $this->set('title_for_layout', 'Hóa đơn bán');
        $this->HoaDonBan->recursive = 0;
        $this->set('hoadonbans', $this->paginate());

    }

    public function add()
    {
        $this->set('title_for_layout', 'Thêm hóa đơn bán');
        //$doituongs = $this->HoaDonNhap->DoiTuongKH->find("list", array('fields' => array('DoiTuongKH.MADT', 'DoiTuongKH.TENDT')));
        //$this->set('doituongs', $doituongs);
        if ($this->request->is('post')) {
            if ($this->HoaDonBan->save($this->request->data)) {
                //$this->Session->setFlash('Đã thêm hóa đơn nhập mới vào danh sách');
                //$MAHDN = $this->data['HoaDonNhap']['MA'];
                //HoaDonNhap::getID($list);
                $MAHDB = $this->HoaDonBan->id;
                $this->Session->write('MAHDB', $MAHDB);
                $this->Session->setFlash($MAHDB);
                $this->redirect(array('controller' => 'chi_tiet_bans', $MAHDB));
            }
        }
    }


    public function view($id = null)
    {
        $this->set('title_for_layout', 'Hóa đơn bán');
        $this->layout = 'thongke';
        $this->HoaDonBan->id = $id;
        $this->set('hoadonban', $this->HoaDonBan->Read());

        $tthd = $this->HoaDonBan->findAllByid($id);
        $manv = $tthd['0']['HoaDonBan']['MANV'];

        $tennv = $this->HoaDonBan->NhanVien->findAllById($manv);
        $this->set('tennv', $tennv);

        $sql = 'select * from doanhnghiep';
        $result = $this->HoaDonBan->query($sql);
        $this->set('doanhnghiep', $result);

        $makh = $tthd['0']['HoaDonBan']['MAKH'];
        $khachhang = $this->HoaDonBan->KhachHang->findAllById($makh);
        $this->set('khachhang', $khachhang);

        $this->set('datra', $tthd['0']['HoaDonBan']['TIENDATRA']);

        $sql_chitietban = 'select * from ChiTietBan a join SanPham b on a.MASP = b.id
                where MAHDB = ' . '"' . $id . '"';
        $result_chitietban = $this->HoaDonBan->query($sql_chitietban);
        $this->set('chitietbans', $result_chitietban);

        $sql_tongtien = 'select sum(SLBAN * DONGIABAN) as ' . '"TongTien"'
            . ' from ChiTietBan where MAHDB = ' . '"' . $id . '"';
        $result_tongtien = $this->HoaDonBan->query($sql_tongtien);
        $this->set('tt', $result_tongtien);

        //print_r($result_tongtien);

        $sql_loaikhachhang = 'select * from khachhang a join doituongkh b on a.doituongkh_id = b.madt where a.id = "' . $makh . '"';
        $result_loaikhachhang = $this->HoaDonBan->query($sql_loaikhachhang);
        $this->set('loaikh', $result_loaikhachhang);


        //Giam gia
        $hoadon = $this->HoaDonBan->findAllById($id);
        $this->set('hoadon', $hoadon);
        $ngayban = $hoadon['0']['HoaDonBan']['NGAYBAN'];
        $this->set('ngayban', $ngayban);
        $maloaikh = $result_loaikhachhang['0']['a']['DOITUONGKH_ID'];

        $sql_giamgia = 'select MUCGIAMGIA
                      from giamgia
                      where doituongkh_id = "' . $maloaikh . '"
                        and tgbd <= "' . $ngayban . '"
                            and (tgkt = "0000-00-00" or tgkt >= "' . $ngayban . '")';
        $result_giamgia = $this->HoaDonBan->query($sql_giamgia);

        /*if ($result_giamgia != null) {
            $this->set('giamgia', $result_giamgia);
        }
        else {
            $this->set('giamgia', 0);
        }*/

       // print_r($tthd);
        if ($result_giamgia != null) {
            $giamgia = $result_giamgia['0']['giamgia']['MUCGIAMGIA'] * $result_tongtien['0']['0']['TongTien'] / 100;
        } else {
            $giamgia = 0;
        }

        $result_tongtien['0']['0']['TongTien'];

        $tiensaugiamgia = $result_tongtien['0']['0']['TongTien'] - $giamgia;

        $tienthue = $tiensaugiamgia * $tthd['0']['HoaDonBan']['THUEBAN'] / 100;

        $tiensauthue = $tiensaugiamgia + $tienthue;

        $this->set('tongtien', $result_tongtien['0']['0']['TongTien']);
        $this->set('giamgia', $giamgia);
        $this->set('tienthue', $tienthue);
        $this->set('tiensauthue', $tiensauthue);

        $sql_tinhtongtien = 'update hoadonban set tongtienhd = ' . $tiensauthue . ' where id = ' . $id;
        $this->HoaDonBan->query($sql_tinhtongtien);

        // $tthd['HoaDonBan']['THUEBAN'];

    }

    public function edit($id = null)
    {
        if ($this->Session->read('quyen') != 'nhanvien') {
            $this->set('title_for_layout', 'Cập nhật hóa đơn bán');
            $this->HoaDonBan->id = $id;
            if ($this->request->is('get')) {
                $this->request->data = $this->HoaDonBan->read();
            } else {
                if ($this->HoaDonBan->save($this->request->data)) {
                    $this->Session->setFlash('Thông tin hóa đơn bán đã được cập nhật');
                    $this->Session->write('MAHDB', $this->HoaDonBan->id);
                    $this->redirect(array('controller' => 'chitietbans', $id));
                }
            }
        } else {
            $this->redirect(array('controller' => '', 'action' => 'index'));
        }
    }

    public function delete($id)
    {

        if ($this->Session->read('quyen') == 'quantri') {
            if (!$this->request->is('post')) {
                throw new MethodNotAllowedException();
            } else {
                if ($this->HoaDonBan->delete($id)) {
                    $this->Session->setFlash('Đã xóa hóa đơn nhập ra khỏi danh sách');
                    $this->redirect(array('action' => 'index'));
                }
            }
        } else {
            $this->redirect(array('controller' => '', 'action' => 'index'));
        }
    }

    public function pay()
    {
        $sql_tratien = 'update hoadonban set tiendatra = ' . $this->request->data['HoaDonBan']['TIENDATRA'] . ' where id = ' . $this->request->data['HoaDonBan']['id'];
        $this->HoaDonBan->query($sql_tratien);
        $this->redirect(array('controller' => 'hoa_don_bans', 'action' => 'view', $this->request->data['HoaDonBan']['id']));

    }
	
	function admin_process(){
		if (!empty($this->data)) {
			$ids = array();              
			foreach($this->data['HoaDonBan']['id'] as $id => $value){
				if($value == 1){
				  $ids[] = $id;
				}
			}
			if (count($ids) == 0) {
				$this->Session->setFlash(__('Không có hóa đơn bán nào được chọn', true));
				$this->redirect(array('action'=>'index'));
			}
				if($this->HoaDonBan->deleteAll(array('HoaDonBan.id'=>$ids))){
						$this->Session->setFlash(__('Hóa Dơn Bán deleted', true));
						$this->redirect(array('action'=>'index'));
				}
		}
	}
}
